﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ThongTinNguoiDung : System.Web.UI.Page
{
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get a reference to the currently logged on user    
        if (Session["g_UserId"] != null)
        {
            Guid currentUserId = (Guid) Session["g_UserId"];
            LoadUser(currentUserId);
        }
    }

    public void LoadUser(Guid id)
    {
        NguoiDungBUS objNguoiDung =
            new NguoiDungBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objNguoiDung.SelectNguoiDung(id);
        if (objDataSet.Tables[0].Rows.Count == 0)
        {
            objNguoiDung = new NguoiDungBUS(TGDDConfiguration.DbConnectionString);
            objNguoiDung.InsertNguoiDung(id);
            return;
        }
            
        DataRow row = objDataSet.Tables[0].Rows[0];
        if (!row["Ten"].ToString().Equals(""))
            txtHoTen.Text = row["Ten"].ToString();
        if (!row["DiaChi"].ToString().Equals(""))
            txtDiaChi.Text = row["DiaChi"].ToString();
        if (!row["GioiTinh"].ToString().Equals(""))
        {
            if (int.Parse(row["GioiTinh"].ToString()) == 1)
            {
                rdlstGioiTinh.Items[0].Selected = true;
            }
            else
            {
                rdlstGioiTinh.Items[1].Selected = true;
            }
        }
        if (!row["NgaySinh"].ToString().Equals(""))
            txtNgaySinh.Text = row["NgaySinh"].ToString();
        if (!row["NgheNghiep"].ToString().Equals(""))
            txtNgheNghiep.Text = row["NgheNghiep"].ToString();
        if (!row["DienThoai"].ToString().Equals(""))
            txtDienThoai.Text = row["DienThoai"].ToString();
    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        NguoiDungBUS objNguoiDung =
            new NguoiDungBUS(TGDDConfiguration.DbConnectionString);
        NguoiDungDTO ndDto = new NguoiDungDTO();
        ndDto.Id = (Guid)Session["g_UserId"];
        ndDto.HoTen = txtHoTen.Text;
        ndDto.DiaChi = txtDiaChi.Text;
        if (rdlstGioiTinh.Items[0].Selected == true)
            ndDto.GioiTinh = 1;
        else
            ndDto.GioiTinh = 0;
        if (!txtNgaySinh.Text.Equals(""))
            ndDto.NgaySinh = DateTime.Parse(txtNgaySinh.Text);
        ndDto.NgheNghiep = txtNgheNghiep.Text;
        ndDto.DienThoai = txtDienThoai.Text;
        if (objNguoiDung.UpdateNguoiDung(ndDto))
        {
            MessageBox("Cập nhật thông tin thành công");

        }
        else
        {
            MessageBox("Cập nhật thông tin thất bại");
        }
    }
}
